package com.langsheng.lsintell.utils.encrypt;

import android.text.TextUtils;

import com.langsheng.lsintell.utils.LSConst;

/**
 * 加密工具类
 * <p>
 * Created by xxw on 2016/8/5.
 */
public class LSEncryptUtil {
    public final static byte[] KEY = LSConst.PSW_KEY.getBytes();

    private static LSEncryptUtil instance = new LSEncryptUtil();
    private byte[] offsetKey;
    private int encryptMode;

    private LSEncryptUtil() {

    }

    public static LSEncryptUtil getInstance() {
        return instance;
    }

    public void reset() {
        offsetKey = null;
        encryptMode = 0;
    }

    public void exit() {
        reset();
    }

    public synchronized void init(String webKey, int encryptMode) {
        if (TextUtils.isEmpty(webKey)) {
            return;
        }

        byte[] tmp;
        try {
            tmp = LSAES256.decryptPwd(webKey);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        this.encryptMode = encryptMode;
        offsetKey = new byte[30];
        int length = tmp.length > 30 ? 30 : tmp.length;
        System.arraycopy(tmp, 0, offsetKey, 0, length);
    }

    public int getEncryptMode() {
        return encryptMode;
    }

    public synchronized byte[] getEncryptKey(byte key1, byte key2) {
        if (offsetKey == null) {
            return KEY;
        }
        byte[] ret = new byte[32];
        ret[0] = key1;
        ret[1] = key2;
        System.arraycopy(offsetKey, 0, ret, 2, offsetKey.length);
        return ret;
    }
}
